define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/stats/models'),
        utils = require('js/utils/tools'),
        moment = require('moment'),
        app = Backbone.history;
    require('datetimepicker');
    var CouponListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click #search': 'doSearch',
            'click #exportData': 'exportData',
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.CouponCollection();
            this.collection.bind('change remove reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        exportData: function (e) {
            e.preventDefault();
            if (this.collection.total == 0) {
                return;
            }
            if (this.collection.total > 1000) {
                notify.warning('警告', '数据量较大，请耐心等待');
            }
            utils.exportData(this.collection.url, this.options);
            return false;
        },
        doSearch: function () {
            var options = utils.getAllInput("#sidebar_right", true),
                searched = utils.getAllInput('#sidebar_right');

            if (searched.id) {
                var id = parseInt(searched.id, 10);
                if (isNaN(id)) {
                    options.title = searched.id;
                    delete options.id;
                }
            }
            if (searched.created_at) {
                options.start_ts = {};
                if (searched.created_at.$gte) options.start_ts.$gte = moment(searched.created_at.$gte, 'YYYY-MM-DD').unix();
                if (searched.created_at.$lt) options.start_ts.$lt = moment(searched.created_at.$lt, 'YYYY-MM-DD').unix();
                delete options.created_at;
            }
            utils.saveSearched('stats/coupon', searched);
            app.navigate(utils.composeQueryString('#stats/coupon/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('stats/coupon'),
                overview: this.collection.overview,
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || '-created_at',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#stats/coupon/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('[data-type=date]').datetimepicker({
                defaultDate: false,
                format: 'YYYY-MM-DD'
            });
            utils.getPaginator(that.options, this.collection.total, '#stats/coupon/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "stats/CouponList.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new CouponListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});